package com.feituan.core.persistence.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feituan.core.persistence.mapper.ProductMapper;
import com.feituan.core.persistence.service.IProductService;
import com.feituan.core.pojo.entity.Product;
import com.feituan.core.pojo.request.CommonPageReq;
import com.feituan.core.pojo.response.page.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaohu
 * @since 2024-05-09
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Override
    public PageData<Product> queryPage(CommonPageReq request) {
        Page<Product> page = this.lambdaQuery()
                .like(!ObjectUtils.isEmpty(request.getName()), Product::getName, request.getName())
                .page(new Page<>(request.getPage(), request.getLimit()));
        return PageData.build(page.getCurrent(), page.getTotal(), page.getRecords());
    }

    /**
     * 根据ids查询商品信息
     * @param ids ids
     */
    @Override
    public List<Product> selectInfoByIds(String ids) {
        if (ids == null || ids.isEmpty()) {
            // 返回空列表而不是null
            return new ArrayList<>();
        }
        // 使用Stream API解析字符串并转换为Long列表
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim) // 去掉可能的空白字符
                .map(Long::valueOf) // 将字符串转换为Long
                .collect(Collectors.toList());
        return productMapper.selectInfoByIds(idList);
    }
}
